package com.ruifu.worksheet.util;

import com.fhzncloud.cloud.common.security.service.CloudUser;
import com.ruifu.worksheet.dto.WsCommonSheetDTO;
import com.ruifu.worksheet.dto.WsCustomizeTypeDTO;

import java.util.ArrayList;
import java.util.List;

/**
 * @author:yuanyundi
 * @create:2020/05/20
 * @desc:权限计算
 */

public class AuthorityUtil {
    /**
     * 工作单查看权限码
     */
    private static final byte[] SEARCH_AUTH = {0,0,0,0,0,0,0,1};

    /**
     * 工作单附件预览权限
     */
    private static final byte[] SHOW_AUTH = {0,0,0,0,0,0,1,0};

    /**
     * 工作单流程跟踪权限
     */
    private static final byte[] FLOW_AUTH = {0,0,0,0,0,1,0,0};

    /**
     * 工作单编辑权限
     */
    private static final byte[] EDIT_AUTH = {0,0,0,0,1,0,0,0};

    /**
     * 工作单删除权限
     */
    private static final byte[] DELETE_AUTH = {0,0,0,1,0,0,0,0};

    /**
     * 工作单附件上传下载权限
     */
    private static final byte[]  DOWNANDUP_AUTH = {0,0,1,0,0,0,0,0};

    /**
     * 工作单提交权限
     */
    private static final byte[]  SUBMIT_AUTH = {0,1,0,0,0,0,0,0};



    /**
     * 计算工作单权限返回码
     * @param statusFlag 流程状态
     * @return 权限返回码
     */
    public  String getWsAuthorCode(WsCommonSheetDTO wsCommonSheetDTO, CloudUser cloudUser,String flowAuthorCode){
        //定义长度
        byte[] result = new byte[8];
        List<byte[]> list = new ArrayList<>();
        if(wsCommonSheetDTO.getCreator() == cloudUser.getId().longValue()){
            //判断流程状态
            if("0".equals(wsCommonSheetDTO.getCmStatus())){
                list.add(SEARCH_AUTH);
                list.add(SHOW_AUTH);
                list.add(EDIT_AUTH);
                list.add(DELETE_AUTH);
                list.add(DOWNANDUP_AUTH);
                list.add(SUBMIT_AUTH);
                result = retXor(list);
            }else if("1".equals(wsCommonSheetDTO.getCmStatus())){
                //判断流程是否处于“返回修改节点”
                if("00010111".equals(flowAuthorCode)){
                    list.add(SEARCH_AUTH);
                    list.add(SHOW_AUTH);
                    list.add(FLOW_AUTH);
                    list.add(EDIT_AUTH);
                    list.add(DOWNANDUP_AUTH);
                }else{
                    list.add(SEARCH_AUTH);
                    list.add(SHOW_AUTH);
                    list.add(FLOW_AUTH);
                }
                result = retXor(list);
            }else{
                list.add(SEARCH_AUTH);
                list.add(SHOW_AUTH);
                list.add(FLOW_AUTH);
                list.add(DOWNANDUP_AUTH);
                result = retXor(list);
            }
        }else{
                list.add(SEARCH_AUTH);
                list.add(SHOW_AUTH);
                if(!"0".equals(wsCommonSheetDTO.getCmStatus())){
                    list.add(FLOW_AUTH);
                }
                result = retXor(list);
        }
                return convertStr(result);
    }

    /**
     * 异或
     *
     * @param list
     * @return
     */
    public  byte[] retXor(List<byte[]> list){
        byte[] result = {0,0,0,0,0,0,0,0};
        for(byte[] t:list){
            for(int i=0;i <t.length; i++){
                result[i] =(byte)(result[i] ^ t[i]);
            }
        }
        return result;
    }

    /**
     * byte转字符串
     *
     * @param result
     * @return
     */
    public  String convertStr(byte[] result){
        String retStr = "";
        for(byte i:result){
            retStr += i;
        }
        return retStr;
    }

    /**
     * 计算工作单类型权限代码
     *
     * @param wsCustomizeTypeDTO 变更单类型
     * @param cloudUser 登录人员信息
     * @return String 权限码
     */
    public  String getWsTypeAuthorCode(WsCustomizeTypeDTO wsCustomizeTypeDTO, CloudUser cloudUser){
        //定义长度
        byte[] result = new byte[8];
        List<byte[]> list = new ArrayList<>();
        if(wsCustomizeTypeDTO.getCreaTor() == cloudUser.getId().longValue()){
            list.add(SEARCH_AUTH);
            list.add(EDIT_AUTH);
            list.add(DOWNANDUP_AUTH);
            list.add(DELETE_AUTH);
        }else{
            list.add(SEARCH_AUTH);
        }
            result = retXor(list);
        return convertStr(result);
    }
}
